package ltd.dujiabao.billingservice.dao;

import ltd.dujiabao.billingservice.domain.entity.AccountBalance;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author Bowie Du
 * @date 2025/7/14 22:23
 */

@Repository
public interface AccountBalanceRepository extends CrudRepository<AccountBalance, Long> {
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Transactional(rollbackFor = Exception.class)
    @Query("update AccountBalance set balance = balance - :amount where userId = :userId and balance >= :amount")
    int decrementBalance(Long userId, BigDecimal amount);
}
